package com.ck.company.controller.client;

import com.ck.annotation.PermissionRequired;
import com.ck.company.service.CpDeviceInfoService;
import com.ck.constant.*;
import com.ck.model.common.User;
import com.ck.model.common.sys.JsonResponse;
import com.ck.model.company.vo.CpProgramPutinDeviceListVO;
import com.ck.util.DateUtils;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 节目编辑-终端选择
 *
 * @author linqiang
 * @version v1.0
 * @date 2018-10-09 16:24
 */
@PermissionRequired
@Controller
@RequestMapping("/client/device")
public class CpDeviceInfoClientController {

    private Logger logger = LoggerFactory.getLogger(CpDeviceInfoClientController.class);

    @Autowired
    private CpDeviceInfoService cpDeviceInfoService;

    /**
     * 跳转到选择终端code页面（投放时间必选，如果未选则默认无限制）
     *
     * @param request       请求
     * @param deviceCode    终端机器码
     * @param deviceGroupId 分组ID
     * @param province      省
     * @param city          市
     * @param area          区
     * @param startTime     投放开始时间
     * @param endTime       投放结束时间
     * @param putinLevel    投放级别（1普通，2插播）
     * @param pageNum       当前页
     * @param pageSize      每页条数
     * @return 页面
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(HttpServletRequest request, String deviceCode, Long deviceGroupId,
                       String province, String city, String area, String startTime,
                       String endTime, Integer putinLevel, Integer pageNum, Integer pageSize) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            User user = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
            String companyCode = user.getCompanyCode();
            pageNum = (pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum);
            pageSize = (pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize);
            Long startTimeL = null;
            Long endTimeL = null;
            if (StringUtils.isNotBlank(startTime)) {
                startTimeL = DateUtils.stringTimeToLongTimeStamp(startTime, DateUtils.PATTEN_YMD_HMS1);
            } else {
                startTimeL = ProgramPutinConstant.UNLIMIT_START_TIME;
            }
            if (StringUtils.isNotBlank(endTime)) {
                endTimeL = DateUtils.stringTimeToLongTimeStamp(endTime, DateUtils.PATTEN_YMD_HMS1);
            } else {
                endTimeL = ProgramPutinConstant.UNLIMIT_END_TIME;
            }
            Page<CpProgramPutinDeviceListVO> page = cpDeviceInfoService.listProgramPutinDeviceBySingleGroupByPage(companyCode,
                    deviceCode, deviceGroupId, province, city, area, startTimeL, endTimeL, putinLevel, pageNum, pageSize);
            result.put("data", page.getResult());
            result.put("pageNum", pageNum);
            result.put("pageSize", pageSize);
            result.put("total", page.getTotal());
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, result, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpDeviceInfoClientController list fail:" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "查询失败");
    }

}
